package adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.example.administrator.text.Framgnet.VPHomeFragment;
import java.util.ArrayList;
import java.util.List;
import entity.HomeZhuYe;
import util.LogUtil;
import view.LoopViewPager;

/**
 * Created by Administrator on 2016/5/13.
 *首页的ViewPager的适配器
 */
public class ViewPagerHome extends FragmentPagerAdapter{

    private ArrayList<HomeZhuYe.BannerModelsBean.PicModelsBean> mImagelist;
    private ArrayList<HomeZhuYe.BannerModelsBean.TextModelsBean> mList;
    private FragmentManager mFragmentManager;
    private List<Fragment> listFragment = new ArrayList<>();
    public ViewPagerHome(FragmentManager fm, List<HomeZhuYe.BannerModelsBean.TextModelsBean> list, List<HomeZhuYe.BannerModelsBean.PicModelsBean> imagelist){
        super(fm);
        this.mList = (ArrayList<HomeZhuYe.BannerModelsBean.TextModelsBean>) list;
        this.mImagelist =(ArrayList<HomeZhuYe.BannerModelsBean.PicModelsBean>)imagelist;
        this.mFragmentManager = fm;
    }

    @Override public Fragment getItem(int position) {
        int positio = LoopViewPager.toRealPosition(position+1, getCount());
        VPHomeFragment homeFragment = VPHomeFragment.newInstance(mImagelist,mList,positio);
        listFragment.add(homeFragment);
        LogUtil.eE("IMAGEPIC","getItem()。。。"+homeFragment);
        return homeFragment;
    }

    @Override public int getCount() {

        if(mList.size() >0){
                return mImagelist.size()+1;
        }
        return mImagelist.size();
    }

    /**
     * 移除缓存
     */
    public void getRemoveFM(){
        if(listFragment !=null && mFragmentManager!=null){
            for (int i = 0; i <listFragment.size(); i++) {
                mFragmentManager.beginTransaction().remove(listFragment.get(i)).commitAllowingStateLoss();
            }
            listFragment.clear();
        }
    }
}
